﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPlot;
using System.Drawing;
using System.Windows.Forms;

namespace RemoteMeter
{
    public class Sensor
    {
        public string sensorName;
        public double V;
        public bool DC = true;
        public int lastRead = 0;
        public List<int> readings = new List<int>();
        public List<double> notRunning = new List<double>();
        public List<double> running = new List<double>();

        public List<double> valoriScarico;
        public List<double> valoriCarico;
        public double mediaScarico;
        public double mediaCarico;
        public double varianzaScarico;
        public double varianzaCarico;
        public List<double> data = new List<double>();

        // per disegnare...
        public TableLayoutPanel panel;
        public List<double> programStartArray;
        public List<double> programStopArray;
        private  NumericUpDown numericUpDownV;
        private ComboBox comboV;
        private TextBox textBoxName;
        private NPlot.Windows.PlotSurface2D plotSurface;
        public Sensor(string name)
        {
            this.sensorName = name;
            panel = new TableLayoutPanel();

            panel.RowCount = 2;
            panel.ColumnCount = 4;

            Label l_name = new Label();
            l_name.Text = "name";
            panel.Controls.Add(l_name);
            panel.SetColumn(l_name, 0);
            panel.SetRow(l_name, 0);

            textBoxName = new TextBox();
            textBoxName.Text = sensorName;
            panel.Controls.Add(textBoxName);
            panel.SetColumn(textBoxName, 1);
            panel.SetRow(textBoxName, 0);
            textBoxName.TextChanged += new EventHandler(textBoxName_TextChanged);

            Label l_V = new Label();
            l_V.Text = "V";
            panel.Controls.Add(l_V);
            panel.SetColumn(l_V, 2);
            panel.SetRow(l_V, 0);

            comboV = new ComboBox();
            comboV.Items.Add((double)3.3);
            comboV.Items.Add((double)5);
            comboV.Items.Add((double)12);
            comboV.SelectedIndex = 0;
            panel.Controls.Add(comboV);
            panel.SetColumn(comboV, 3);
            panel.SetRow(comboV, 0);
            V = (double) comboV.SelectedItem;

            comboV.SelectedValueChanged += new EventHandler(comboV_SelectedValueChanged);

            Panel p = new Panel();
            //p.Width = 700;
            //p.Height = 300;
            panel.Controls.Add(p);
            panel.SetColumn(p, 0);
            panel.SetRow(p, 1);
            panel.SetColumnSpan(p, 4);
            p.Dock = DockStyle.Fill;
            // NPlot
            this.plotSurface = new NPlot.Windows.PlotSurface2D();
            this.plotSurface.AutoScaleAutoGeneratedAxes = false;
            this.plotSurface.AutoScaleTitle = false;
            this.plotSurface.BackColor = System.Drawing.SystemColors.Control;
            this.plotSurface.Legend = null;
            this.plotSurface.Location = new System.Drawing.Point(8, 8);
            this.plotSurface.Name = "plotSurface";
            this.plotSurface.RightMenu = null;
            this.plotSurface.ShowCoordinates = false;
            this.plotSurface.Size = new System.Drawing.Size(616, 360);
            this.plotSurface.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
            this.plotSurface.TabIndex = 13;
            this.plotSurface.Title = "";
            this.plotSurface.TitleFont = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.plotSurface.XAxis1 = null;
            this.plotSurface.XAxis2 = null;
            this.plotSurface.YAxis1 = null;
            this.plotSurface.YAxis2 = null;

            this.plotSurface.Anchor =
                System.Windows.Forms.AnchorStyles.Left |
                System.Windows.Forms.AnchorStyles.Right |
                System.Windows.Forms.AnchorStyles.Top |
                System.Windows.Forms.AnchorStyles.Bottom;
            //this.Resize += new System.EventHandler(this.ResizeHandler);

            //panel1.Controls.Add(plotSurface);

            plotSurface.XAxis1 = new Axis();
            plotSurface.Dock = DockStyle.Fill;

            p.Controls.Add(plotSurface);

            //PlotWave();
            programStopArray = new List<double>();
            programStartArray = new List<double>();
        }

        void comboV_SelectedValueChanged(object sender, EventArgs e)
        {
            this.V = (double) comboV.SelectedItem;
        }

        void textBoxName_TextChanged(object sender, EventArgs e)
        {
            this.sensorName = textBoxName.Text;
        }

        public void AvviaSetTests()
        {
            programStartArray = new List<double>();
            programStopArray = new List<double>();
        }

        public void PlotWave()
        {
            //string[] lines = {
            //    "Sound Wave Example. Demonstrates - ",
            //    "  * StepPlot (centered) and HorizontalLine IDrawables",
            //    "  * How to set colors of various things.",
            //    "  * A few plot interactions. Try left clicking and dragging (a) the axes (b) in the plot region.",
            //    "  * In the future I plan add a plot interaction for axis drag that knows if the ctr key is down. This will select between drag/scale" };
            //infoBox.Lines = lines;

            try
            {

                plotSurface.Clear();
                plotSurface.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.VerticalGuideline());
                plotSurface.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.HorizontalRangeSelection(3));
                plotSurface.AddInteraction(new NPlot.Windows.PlotSurface2D.Interactions.AxisDrag(true));

                plotSurface.Add(new HorizontalLine(0.0, Color.LightBlue));

                StepPlot sp = new StepPlot();
                sp.DataSource = data;
                sp.Color = Color.Yellow;
                sp.Center = true;
                plotSurface.Add(sp);

                plotSurface.YAxis1.FlipTicksLabel = true;

                plotSurface.PlotBackColor = Color.DarkBlue;
                plotSurface.BackColor = Color.Black;

                plotSurface.XAxis1 = new LinearAxis(0.0, (double)data.Count);

                plotSurface.XAxis1.Color = Color.White;
                plotSurface.YAxis1.Color = Color.White;

                foreach (double line in programStartArray)
                {
                    plotSurface.Add(new VerticalLine((double)line, Color.Green));

                }
                foreach (double line in programStopArray)
                {
                    plotSurface.Add(new VerticalLine((double)line, Color.Red));

                }

                //plotSurface.Add(new HorizontalLine(programNotRunningMean, Color.Orange));
                //plotSurface.Add(new HorizontalLine(programRunningMean, Color.Purple));
                plotSurface.ShowCoordinates = true;
                plotSurface.Refresh();
            }
            catch (Exception ex)
            {
            }

        }

        public void SetV(double v)
        {
            this.V = v;
            if (v == 3.3)
            {
                comboV.SelectedIndex = 0;
            }
            if (v == 5)
            {
                comboV.SelectedIndex = 1;
            }
            if (v == 12)
            {
                comboV.SelectedIndex = 2;
            }
        }

    }
}
